starter.controller('busCodeController', function($rootScope,$state,$ionicPopup,$timeout,$scope,Page, localStore,$location) {


    var orderId = $state.params.orderId;
    if(orderId == undefined || orderId == "") {
        Page.notice("非法请求!");
        return;
    }

    Page.ajaxPost("app/order/details.do",{orderId : orderId},function(resp){
        $scope.order = resp.data;
        if($scope.order.passengers.length > 0) {
            $scope.stepCount = 0;
            $scope.passenger = $scope.order.passengers[0];
            $("#busCodeDiv").empty();
            if($scope.passenger.dates[0].status != "3") {
                Page.notice($scope.passenger.passengerName + " : " + $scope.passenger.dates[0].date + "已申请退款!");
                return;
            }
            Page.ajaxPost("app/order/busCode.do",$scope.passenger,function(resp){
                makeCode(resp.data.orderStr);
            });
        }
    });

    //生成二维码
    function makeCode(orderStr) {
        $('#busCodeDiv').qrcode({
            render: 'canvas',
            text: orderStr,
            height: 200,
            width: $rootScope.screenWidth - 80,
            //logo图片地址
            src: 'img/wx_logo.png'
        });
    }

    $scope.lastPassenger = function() {
        if($scope.stepCount == 0) {
            //没有上一个乘客，直接disabled
            return;
        }
        $scope.stepCount = $scope.stepCount - 1;
        $scope.passenger = $scope.order.passengers[$scope.stepCount];
        $("#busCodeDiv").empty();
        if($scope.passenger.dates[0].status != "3") {
            Page.notice($scope.passenger.passengerName + " : " + $scope.passenger.dates[0].date + "已申请退款!");
            return;
        }
        Page.ajaxPost("app/order/busCode.do",$scope.passenger,function(resp){
            makeCode(resp.data.orderStr);
        });

    };

    $scope.nextPassenger = function() {
        if($scope.stepCount == $scope.order.passengers.length - 1) {
            //没有下一个乘客，直接disabled
            return;
        }
        $scope.stepCount = $scope.stepCount + 1;
        $scope.passenger = $scope.order.passengers[$scope.stepCount];
        $("#busCodeDiv").empty();
        if($scope.passenger.dates[0].status != "3") {
            Page.notice($scope.passenger.passengerName + " : " + $scope.passenger.dates[0].date + "已申请退款!");
            return;
        }
        Page.ajaxPost("app/order/busCode.do",$scope.passenger,function(resp){
            makeCode(resp.data.orderStr);
        });
    };

    $scope.refreshCode = function() {
        $("#busCodeDiv").empty();
        if($scope.passenger.dates[0].status != "3") {
            Page.notice($scope.passenger.passengerName + " : " + $scope.passenger.dates[0].date + "已申请退款!");
            return;
        }
        Page.ajaxPost("app/order/busCode.do",$scope.passenger,function(resp){
            makeCode(resp.data.orderStr);
        });
    };


    $scope.backGo = function() {
        $state.go("order_hadPay",{orderId : $scope.order.orderId})
    }

});